package com.coda.clouddisk.service.impl;

import com.coda.clouddisk.context.BaseContext;
import com.coda.clouddisk.entity.*;
import com.coda.clouddisk.exception.BaseException;
import com.coda.clouddisk.mapper.FileShareCordFriendMapper;
import com.coda.clouddisk.mapper.FileShareRecordMapper;
import com.coda.clouddisk.mapper.SuperMapper;
import com.coda.clouddisk.message.MessageContext;
import com.coda.clouddisk.service.SuperService;
import com.coda.clouddisk.service.inter.FileShareRecordService;
import com.coda.clouddisk.utils.EntityToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Time;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class FileShareRecordServiceImpl extends SuperService implements FileShareRecordService {
    @Autowired
    private FileShareRecordMapper fileShareRecordMapper;
    @Autowired
    private FileShareCordFriendMapper fileShareCordFriendMapper;

    @Override
    public SuperMapper getMapper() {
        return fileShareRecordMapper;
    }


    /**
     * 创建文件分享
     *
     * @param fileShareRecord
     * @return
     */
    @Override
    @Transactional
    public void createFileShare(FileShareRecord fileShareRecord) {
        //设置分享人数
        fileShareRecord.setShareCount(1);
        //设置文件所属用户
        User user = new User();
        user.setId(BaseContext.getCurrentId());
        fileShareRecord.setUser(user);
        fileShareRecord.setBusinessDate(new Date());
        //保存文件共享数据
        try {
            Map<String, Object> map = EntityToMap.convertObjectToMap(fileShareRecord);
            ArrayList<String> fileList = fileShareRecord.getFile();
            map.remove("file");
            for (String file : fileList) {
                String id = UUID.randomUUID().toString().substring(0, 8).replace("-", "");
                //设置id
                map.put("id", id);
                map.put("file", file);
                map.put("shareCode",getShareCode());
                fileShareRecordMapper.add(map);   //保存共享记录
                map.remove("id");
                map.remove("file");
                map.remove("shareCode");

                for (String friend : fileShareRecord.getFriendId()) {
                    HashMap<String, Object> map1 = new HashMap<>();
                    map1.put("parent",id);
                    map1.put("friend",friend);
                    fileShareCordFriendMapper.add(map1);   //添加文件好友关联数据
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new BaseException(MessageContext.ERROR_CODE, "文件共享失败"+e.getMessage());
        }
    }

    /**
     * 获取分享的文件
     *
     * @return
     */
    @Override
    public List<FileShareRecord> getShareFile(Map<String,Object> data) {
        data.put("time",LocalDateTime.now());
        data.put("status","1");
        data.put("userId", BaseContext.getCurrentId());
        //集合类型转换
        List<FileShareRecord> fileShareFile = fileShareRecordMapper.findAll(data).stream()
                .map(item -> (FileShareRecord) item) // 这里需要确保item可以安全地转换为KeSunAttachment
                .collect(Collectors.toList());
        return fileShareFile;
    }

    /**
     * 删除文件共享记录
     */
    @Override
    public KesunReturn delete(ArrayList<String> ids) {
        //删除文件共享中间表数据
        int count1 = fileShareCordFriendMapper.deletes(ids);
        //删除文件共享记录
        int count2 = fileShareRecordMapper.deletes(ids);

        KesunReturn kesunReturn = new KesunReturn();
        kesunReturn.setCode(MessageContext.SUCCESS_CODE);
        kesunReturn.setMsg("删除成功");
        return kesunReturn;
    }

    /**
     * 生成文件共享码
     */
    private String getShareCode() {
        Random rd = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            sb.append(rd.nextInt(10));
        }
        return sb.toString();
    }


    /**
     * 查看分享出去的文件
     * @param data
     * @return
     */
    public List<FileShareRecord> getshareFileOut(Map<String, Object> data) {
        data.put("userId",BaseContext.getCurrentId());
        List<FileShareRecord> keSunAttachmentList = fileShareRecordMapper.getShareFileOut(data);
        return keSunAttachmentList;
    }
}
